package View.Dorm;

import Service.DormService;
import Service.ServiceImpl.DormServiceImpl;
import View.Index.Index_admin;
import View.Student.QueryStudent;
import domain.Dorm;
import domain.User;

import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.List;

public class QueryDorm extends JFrame {


    public QueryDorm(User user) {
        init();
        componentInit(user);
    }

    public void init() {
        this.setTitle("Rpofop");
        this.setSize(1700, 518);
        this.setResizable(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(null);
    }

    public void componentInit(User user) {
        //------------------JLabel--------------------
        JLabel label1 = new JLabel();
        label1.setText("宿舍号:");
        label1.setBounds(40, 18, 65, 30);
        JLabel label2 = new JLabel();
        label2.setText("成员1:");
        label2.setBounds(222, 18, 65, 30);
        JLabel label3 = new JLabel();
        label3.setText("成员2:");
        label3.setBounds(422, 19, 65, 30);
        JLabel label4 = new JLabel();
        label4.setText("成员3:");
        label4.setBounds(623, 20, 65, 30);
        JLabel label5 = new JLabel();
        label5.setText("成员4:");
        label5.setBounds(843, 20, 65, 30);
        JLabel label6 = new JLabel();
        label6.setText("成员5:");
        label6.setBounds(1040, 20, 65, 30);
        JLabel label7 = new JLabel();
        label7.setText("成员6:");
        label7.setBounds(1240, 20, 65, 30);
        JLabel label8 = new JLabel();
        label8.setText("宿舍长:");
        label8.setBounds(1440, 20, 65, 30);

        //------------------JTextField--------------------
        JTextField field1 = new JTextField();
        field1.setText("");
        field1.setBorder(new LineBorder(Color.lightGray));
        field1.setBounds(90, 18, 120, 30);
        JTextField field2 = new JTextField();
        field2.setText("");
        field2.setBorder(new LineBorder(Color.lightGray));
        field2.setBounds(293, 19, 120, 30);
        JTextField field3 = new JTextField();
        field3.setText("");
        field3.setBorder(new LineBorder(Color.lightGray));
        field3.setBounds(493, 19, 120, 30);
        JTextField field4 = new JTextField();
        field4.setText("");
        field4.setBorder(new LineBorder(Color.lightGray));
        field4.setBounds(699, 20, 120, 30);
        JTextField field5 = new JTextField();
        field5.setText("");
        field5.setBorder(new LineBorder(Color.lightGray));
        field5.setBounds(899, 20, 120, 30);
        JTextField field6 = new JTextField();
        field6.setText("");
        field6.setBorder(new LineBorder(Color.lightGray));
        field6.setBounds(1100, 20, 120, 30);
        JTextField field7 = new JTextField();
        field7.setText("");
        field7.setBorder(new LineBorder(Color.lightGray));
        field7.setBounds(1300, 20, 120, 30);
        JTextField field8 = new JTextField();
        field8.setText("");
        field8.setBorder(new LineBorder(Color.lightGray));
        field8.setBounds(1500, 20, 120, 30);

        //------------------JButton--------------------
        JButton button1 = new JButton();
        button1.setText("查询");
        button1.setBounds(600, 67, 175, 43);
        JButton button2 = new JButton();
        button2.setText("退出");
        button2.setBounds(900, 67, 175, 43);

        JScrollPane panel = new JScrollPane();
        panel.setBounds(100, 120, 1500, 350);


        this.add(label1);
        this.add(label2);
        this.add(label3);
        this.add(label4);
        this.add(label5);
        this.add(label6);
        this.add(label7);
        this.add(label8);
        this.add(field1);
        this.add(field2);
        this.add(field3);
        this.add(field4);
        this.add(field5);
        this.add(field6);
        this.add(field7);
        this.add(field8);
        this.add(button1);
        this.add(button2);
        this.add(panel);

        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String[] title = {"宿舍号","成员1","成员2","成员3","成员4","成员5","成员6","水费","电费","宿舍长","宿舍评级"};
            if(!"".equals(field1) && !"".equals(field2) && !"".equals(field3) && !"".equals(field4) && !"".equals(field5) && !"".equals(field6) && !"".equals(field7) && !"".equals(field8)){
                HashMap<String,String> map = new HashMap<>();
                map.put("dormnum",field1.getText());
                map.put("member1",field2.getText());
                map.put("member2",field3.getText());
                map.put("member3",field4.getText());
                map.put("member4",field5.getText());
                map.put("member5",field6.getText());
                map.put("member6",field7.getText());
                map.put("head",field8.getText());

                DormService service = new DormServiceImpl();
                List<Dorm> dorms = service.fuzzySelectDorm(map);
                System.out.println(dorms);
                Object[][] neirong = new Object[dorms.size()][11];
                for (int i = 0; i < dorms.size(); i++) {
                    Dorm dorm = dorms.get(i);

                    neirong[i][0]=dorm.getDormnum();
                    neirong[i][1]=dorm.getMember1();
                    neirong[i][2]=dorm.getMember2();
                    neirong[i][3]=dorm.getMember3();
                    neirong[i][4]=dorm.getMember4();
                    neirong[i][5]=dorm.getMember5();
                    neirong[i][6]=dorm.getMember6();
                    neirong[i][7]=dorm.getWater();
                    neirong[i][8]=dorm.getEle();
                    neirong[i][9]=dorm.getHead();
                    neirong[i][10]=dorm.getLevel();

                }
                JTable table = new JTable(neirong, title);
                DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();
                tcr.setHorizontalAlignment(SwingConstants.CENTER);
                table.setDefaultRenderer(Object.class, tcr);
                panel.setViewportView(table);
            }else {
                DormService service = new DormServiceImpl();
                List<Dorm> dorms = service.dormList();

                Object[][] neirong = new Object[dorms.size()][11];
                for (int i = 0; i < dorms.size(); i++) {
                    Dorm dorm = dorms.get(i);

                    neirong[i][0]=dorm.getDormnum();
                    neirong[i][1]=dorm.getMember1();
                    neirong[i][2]=dorm.getMember2();
                    neirong[i][3]=dorm.getMember3();
                    neirong[i][4]=dorm.getMember4();
                    neirong[i][5]=dorm.getMember5();
                    neirong[i][6]=dorm.getMember6();
                    neirong[i][7]=dorm.getWater();
                    neirong[i][8]=dorm.getEle();
                    neirong[i][9]=dorm.getHead();
                    neirong[i][10]=dorm.getLevel();

                }
                JTable table = new JTable(neirong, title);
                DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();
                tcr.setHorizontalAlignment(SwingConstants.CENTER);
                table.setDefaultRenderer(Object.class, tcr);
                panel.setViewportView(table);
            }
            }
        });

        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
                new Index_admin(user).setVisible(true);
            }
        });
    }
}